using System.IO;
using System.Xml.Serialization;

namespace BizTalk.BuildGenerator.UI
{
    public class ArgsHelper
    {
        /// <summary>
        /// Saves the args to a file
        /// </summary>
        /// <param name="path"></param>
        /// <param name="args"></param>
        public static void Save(string path, GenerationArgs args)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                XmlSerializer serializer = new XmlSerializer(typeof (GenerationArgs));
                serializer.Serialize(fs, args);
                fs.Flush();
                fs.Close();
            }
        }

        /// <summary>
        /// Opens a file and deserializes it to the generation args
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static GenerationArgs Open(string path)
        {
            GenerationArgs args = null;

            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                XmlSerializer serializer = new XmlSerializer(typeof (GenerationArgs));
                args = (GenerationArgs) serializer.Deserialize(fs);
                fs.Close();
            }

            return args;
        }
    }
}